package com.rndemo;

import android.util.Log;
import android.view.KeyEvent;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "RNDemo";
    }

    protected ReactActivityDelegate createReactActivityDelegate() {
        return new ReactActivityDelegate(this, getMainComponentName()) {

            /**
             * 通过 adb 发 M 按键事件通知 app 弹出dev对话框、发 R 按键事件通知 app reload
             */
            @Override
            public boolean onKeyUp(int keyCode, KeyEvent event) {
                if (getReactNativeHost().hasInstance() && getReactNativeHost().getUseDeveloperSupport()) {
                    if (keyCode == KeyEvent.KEYCODE_M) {
                        Log.d("===RN===", "KEYCODE_M");
                        getReactNativeHost().getReactInstanceManager().getDevSupportManager().showDevOptionsDialog();
                        return true;
                    }
                    if (keyCode == KeyEvent.KEYCODE_R) {
                        Log.d("===RN===", "KEYCODE_R");
                        getReactNativeHost().getReactInstanceManager().getDevSupportManager().handleReloadJS();
                        return true;
                    }
                }
                return super.onKeyUp(keyCode, event);
            }
        };
    }

}
